﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StandardManager.EntityCode
{
    public partial class LocationCodeForm : Form
    {
        public string CodeFieldName { get; set; }
        public string LocationFieldName { get; set; }

        public LocationCodeForm(DataTable dt, string locationEncodingField, string locationField)
        {
            CodeFieldName = locationEncodingField;
            LocationFieldName = locationField;
            InitializeComponent();
            InitComBobox(dt);
            this.txtFieldName.Text = string.IsNullOrEmpty(CodeFieldName) ? txtFieldName.Text : CodeFieldName;
        }

        private void InitComBobox(DataTable dt)
        {
            cmbField.DropDownStyle = ComboBoxStyle.DropDownList;
            foreach (DataColumn column in dt.Columns)
            {
                cmbField.Items.Add(column.ColumnName);
            }
            if (!string.IsNullOrEmpty(LocationFieldName) && cmbField.Items.Contains(LocationFieldName))
            {
                cmbField.SelectedIndex = cmbField.Items.IndexOf(LocationFieldName);
            }
            else 
            {
                cmbField.SelectedIndex = -1;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CodeFieldName = txtFieldName.Text;
            LocationFieldName = cmbField.SelectedItem.ToString();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
